﻿using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using HandyControl.Controls;
using WWLauncher.Constant;
using WWLauncher.Util;
using WWLauncher.View.UserControls.PannelCard;
using WWLauncher.ViewModel;

namespace WWLauncher.View.Other
{
    /// <summary>
    /// SearchResControl.xaml 的交互逻辑
    /// </summary>
    public partial class SearchResControl : UserControl
    {
        readonly AppConfig Config = MainViewModel.Instance().AppConfig;
        public RightCardControl RightCard = new();
        public ProcessUtil process = new();
        MainWindow mainWindow;
        public SearchResControl(ObservableCollection<IconInfo> iconList, MainWindow window)
        {
            this.DataContext = iconList;
            InitializeComponent();
            mainWindow = window;
            process.Window = window;
            process.AutoHidden = window.autoHidden;
        }



        public void SearchListBoxIndexAdd()
        {
            //控制移动后 鼠标即使在图标上也不显示popup
            RunTimeStatus.MouseMoveCount = 0;
            RightCard.MyPoptip.IsOpen = false;

            if (SearchListBox.Items.Count > 0)
            {
                if (SearchListBox.SelectedIndex < SearchListBox.Items.Count - 1)
                {
                    SearchListBox.SelectedIndex += 1;
                }
            }
        }

        public void SearchListBoxIndexSub()
        {
            //控制移动后 鼠标即使在图标上也不显示popup
            RunTimeStatus.MouseMoveCount = 0;
            RightCard.MyPoptip.IsOpen = false;

            if (SearchListBox.Items.Count > 0)
            {
                if (SearchListBox.SelectedIndex > 0)
                {
                    SearchListBox.SelectedIndex -= 1;
                }
            }
        }

        public void StartupSelectionItem()
        {
            if (SearchListBox.SelectedItem != null)
            {
                IconInfo icon = SearchListBox.SelectedItem as IconInfo;
                ProcessEnum pp = process.StartIconApp(icon);

                if (pp != ProcessEnum.Success)
                {
                    Growl.Error(pp.GetDescription(), "MainWindowGrowl");
                }
            }
        }

        private void SearchListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SearchListBox.ScrollIntoView(SearchListBox.SelectedItem);
        }



        /// <summary>
        /// 查询结果ICON鼠标移动事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SearchIcon_MouseMove(object sender, MouseEventArgs e)
        {
            //控制首次刷新搜索结果后, 鼠标首次移动后显示popup
            RunTimeStatus.MouseMoveCount++;

            //防止移动后不刷新popup content
            IconInfo info = (sender as Panel).Tag as IconInfo;
            RightCard.MyPoptipContent.Text = info.ToolTip;
            RightCard.MyPoptip.VerticalOffset = 30;

            if (RunTimeStatus.MouseMoveCount > 1 && !RunTimeStatus.IconlistMouseWheel)
            {
                RightCard.MyPoptip.IsOpen = true;
            }
        }

        /// <summary>
        /// 查询结果 ICON 鼠标进入事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SearchIcon_MouseEnter(object sender, MouseEventArgs e)
        {

            //显示popup
            RunTimeStatus.MouseEnterIcon = true;
            if (!RunTimeStatus.IconlistMouseWheel)
            {
                new Thread(() =>
                {
                    this.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        var info = (sender as Panel)?.Tag as IconInfo;
                        RightCard.MyPoptipContent.Text = info.ToolTip;
                        RightCard.MyPoptip.VerticalOffset = 30;
                        Thread.Sleep(100);
                        if (!RunTimeStatus.IconlistMouseWheel && RunTimeStatus.MouseMoveCount > 1)
                        {
                            RightCard.MyPoptip.IsOpen = true;
                        }
                    }));
                }).Start();
            }
        }

        /// <summary>
        /// 查询结果ICON鼠标离开事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SearchIcon_MouseLeave(object sender, MouseEventArgs e)
        {
            RunTimeStatus.MouseEnterIcon = false;
            RightCard.MyPoptip.IsOpen = false;
        }

        /// <summary>
        /// 搜索结果icon 列表鼠标滚轮预处理时间  
        /// 主要使用自定义popup解决卡顿问题解决卡顿问题
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void VerticalIconList_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            //控制在滚动时不显示popup 否则会在低GPU性能机器上造成卡顿
            RightCard.MyPoptip.IsOpen = false;
            if (RunTimeStatus.IconlistMouseWheel)
            {
                RunTimeStatus.MouseWheelWaitMs = 500;
            }
            else
            {
                RunTimeStatus.IconlistMouseWheel = true;

                new Thread(() =>
                {
                    while (RunTimeStatus.MouseWheelWaitMs > 0)
                    {
                        Thread.Sleep(1);
                        RunTimeStatus.MouseWheelWaitMs -= 1;
                    }
                    if (RunTimeStatus.MouseEnterIcon)
                    {
                        this.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            RightCard.MyPoptip.IsOpen = true;
                        }));
                    }
                    RunTimeStatus.MouseWheelWaitMs = 100;
                    RunTimeStatus.IconlistMouseWheel = false;
                }).Start();
            }
        }


        private void Icon_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (Config.DoubleOpen)
            {
                IconClick(sender, e);
            }
        }

        private void Icon_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (!Config.DoubleOpen)
            {
                IconClick(sender, e);
            }
        }

        /// <summary>
        /// 图标点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void IconClick(object sender, MouseButtonEventArgs e)
        {
            this.RightCard.IconClick(sender, e);
        }


        private void VerticalCard_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {

        }



        /// <summary>
        /// 管理员身份启动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void IconAdminStart_Click(object sender, RoutedEventArgs e)
        {
            IconInfo icon = (IconInfo)((MenuItem)sender).Tag;
            process.StartIconApp(icon);
        }

        /// <summary>
        /// 打开文件所在位置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ShowInExplore_Click(object sender, RoutedEventArgs e)
        {
            IconInfo icon = (IconInfo)((MenuItem)sender).Tag;
            process.StartIconApp(icon);
        }


        private void SystemContextMenu_Click(object sender, RoutedEventArgs e)
        {
            IconInfo icon = (IconInfo)((MenuItem)sender).Tag;
            DirectoryInfo[] folders = new DirectoryInfo[1];
            folders[0] = new DirectoryInfo(icon.TargetPath);
            ShellContextMenu scm = new ShellContextMenu();
            System.Drawing.Point p = System.Windows.Forms.Cursor.Position;
            p.X -= 80;
            p.Y -= 80;
            scm.ShowContextMenu(folders, p);
        }
    }
}